package com.lfg.leetcode.leetcode200;

public class No213rob {

    public static void main(String[] args) {
        int ret = rob(new int[]{1,3, 1, 3, 100});
        System.out.println(ret);
    }

    public static int rob(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }

        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }

        int[] dp1 = new int[nums.length + 1];
        int[] dp2 = new int[nums.length + 1];
        dp1[0] = nums[0];
        dp1[1] = Math.max(nums[0], nums[1]);

        dp2[1] = nums[1];
        dp2[2] = Math.max(nums[2], nums[1]);
        for (int i = 2; i < nums.length-1; i++) {
            dp1[i] = Math.max(dp1[i - 1], dp1[i - 2] + nums[i]);
        }
        for (int i = 3; i < nums.length; i++) {
            dp2[i] = Math.max(dp2[i - 1], dp2[i - 2] + nums[i]);
        }
        return Math.max(dp1[nums.length - 2], dp2[nums.length - 1]);
    }

    public static int rob2(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums.length == 1) {
            return nums[0];
        }
        if (nums.length == 2) {
            return Math.max(nums[0], nums[1]);
        }
        int[] total = new int[nums.length];
        total[1] = nums[1];
        total[2] = nums[2];

        for (int i = 3; i < nums.length - 1; i++) {
            total[i] = Math.max(total[i - 2], total[i - 3]) + nums[i];
        }
        if ((nums.length & 1) == 0) {
            total[nums.length - 2] += nums[0];
        }
        int num = Math.max(nums[0], nums[nums.length - 1]);
        total[nums.length - 1] = Math.max(total[nums.length - 3], total[nums.length - 4]) + num;
        return Math.max(total[nums.length - 2], total[nums.length - 1]);
    }


    public static int rob3(int[] nums) {
        int len = nums.length;
        if (len == 0) return 0;
        if (len == 1) return nums[0];
        if (len == 2) return Math.max(nums[0], nums[1]);

        int[] nums1 = new int[len - 1];
        int[] nums2 = new int[len - 1];
        for (int i = 0; i < len - 1; i++) nums1[i] = nums[i];
        for (int i = 1; i < len; i++) nums2[i - 1] = nums[i];

        return Math.max(robSingle(nums1), robSingle(nums2));
    }

    public static int robSingle(int[] nums) {
        int len = nums.length;
        int[] arrays = new int[len];
        for (int i = 0; i < len; i++) {
            if (i == 0) arrays[i] = nums[i];
            else if (i == 1) arrays[i] = Math.max(nums[i], nums[0]);
            else arrays[i] = Math.max(arrays[i - 1], arrays[i - 2] + nums[i]);
        }

        return arrays[len - 1];

    }


}
